package edu.jsnu.operate.controller.ticket;

import edu.jsnu.operate.controller.ticket.common.AbstractController;
import edu.jsnu.operate.util.CheckCodeUtils;
import edu.jsnu.service.ticket.user.domain.User;
import edu.jsnu.service.ticket.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/login")
public class LoginController extends AbstractController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login.page")
    public String login() {
        return "ticket/login/login";
    }

    @RequestMapping("/register.page")
    public String register() {
        return "ticket/login/register";
    }

    /**
     * 获取图片验证码
     *
     * @param request
     * @param response
     * @param randomStr
     */
    @RequestMapping(value = "verify.json", method = RequestMethod.GET)
    public void doGet(HttpServletRequest request, HttpServletResponse response, @RequestParam(required = false, value = "random") String randomStr) {
        try {
            CheckCodeUtils.getCmsCode(request, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "login.json", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public void login(HttpServletRequest request, HttpServletResponse response, String userName, String password, String code) {
        try {
            String errorMsg = null;
            String url = request.getServletPath();
            User user = userService.getUserInfoByName(userName);
            if (StringUtils.isBlank(code)) {
                errorMsg = "验证码不能为空";
            } else if (!StringUtils.equalsAnyIgnoreCase(CheckCodeUtils.getCmsCode(request), code)) {
                errorMsg = "验证码错误";
            } else if (StringUtils.isBlank(userName)) {
                errorMsg = "用户名不可以为空";
            } else if (StringUtils.isBlank(password)) {
                errorMsg = "密码不可以为空";
            } else if (url == null) {
                errorMsg = "查询不到指定的用户";
            } else if (!user.getPassword().equals(password)) {
                errorMsg = "用户名或密码错误";
            } else if (user.getRoleId() != 1) {//管理员1，普通用户2
                errorMsg = "请核对账号身份再登陆";
            } else {
                // login success
                request.getSession().setAttribute("user", user);
                response.sendRedirect(request.getContextPath() + "/ticket/index/index.page");
                return;
            }
            request.getSession().setAttribute("error", errorMsg);
            response.sendRedirect("login.page");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/register.json", method = RequestMethod.POST)
    @ResponseBody
    public void register(HttpServletRequest request, HttpServletResponse response,
                         String userName, String password, String password2, String code) {
        try {
            String errorMsg = null;
            String url = request.getServletPath();
            User user = userService.getUserInfoByName(userName);
            if (StringUtils.isBlank(code)) {
                errorMsg = "验证码不能为空";
            } else if (!StringUtils.equalsAnyIgnoreCase(CheckCodeUtils.getCmsCode(request), code)) {
                errorMsg = "验证码错误";
            } else if (StringUtils.isBlank(userName)) {
                errorMsg = "用户名不可以为空";
            } else if (StringUtils.isBlank(password)) {
                errorMsg = "密码不可以为空";
            } else if (StringUtils.isBlank(password2)) {
                errorMsg = "确认密码不可以为空";
            } else if (!password.equals(password2)) {
                errorMsg = "两次密码不一致";
            } else if (url == null) {
                errorMsg = "注册失败";
            } else {
                // register success
                request.getSession().setAttribute("user", user);
                response.sendRedirect(request.getContextPath() + "/ticket/index/index.page");
                return;
            }
            request.getSession().setAttribute("error", errorMsg);
            response.sendRedirect("login.page");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/logout.page")
    public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.getSession().invalidate();
        String path = "login.page";
        response.sendRedirect(path);
    }


}
